﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class controls_admin_CategoryEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            base.removeHGC(divMessage);
            if (!IsPostBack)
            {
                bindDDL(ddlContentType, BO_portal_content_types.SelectAll(1), "DESCRIPTION", "CONTENT_TYPE_ID");
                bindDDL(ddlParent, BO_portal_categories.Inside(0), "FULL_CATEGORY_NAME", "CATEGORY_ID");
                if (Id < 0)
                    Response.Redirect(Request.UrlReferrer.AbsolutePath);
                loadData();
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message); }
    }
    protected void loadData()
    {
        ObjDT = BO_portal_categories.Search(Id);
        ddlParent.Items.Remove(ddlParent.Items.FindByValue(Id.ToString()));
        ddlParent.SelectedValue = ObjDT.Rows[0]["PARENT_ID"].ToString();
        ddlContentType.SelectedValue = ObjDT.Rows[0]["CONTENT_TYPE_ID"].ToString();
        txtFullCategoryName.Text = ObjDT.Rows[0]["FULL_CATEGORY_NAME"].ToString();
        txtUrl.Text = ObjDT.Rows[0]["FULL_CATEGORY_NAME_ENG"].ToString();
        txtFullCategoryDescription.Text = ObjDT.Rows[0]["FULL_CATEGORY_DESCRIPTION"].ToString();
        ctImg.Src = "/productscategories/" + ObjDT.Rows[0]["THUMB_ICON"].ToString();
        txtSortDisplay.Text = ObjDT.Rows[0]["ORDER_DISPLAY"].ToString();
        cboMenu.Checked = Convert.ToBoolean(ObjDT.Rows[0]["IS_MENU"].ToString());
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ObjDT = BO_portal_categories.Search(Id);
            string filename = ObjDT.Rows[0]["THUMB_ICON"].ToString();
            if (ObjDT != null && ObjDT.Rows.Count > 0)
            {
                string categoryName = txtFullCategoryName.Text.Trim();
                string categoryNameEng = txtUrl.Text.Trim();
                string categoryDescription = txtFullCategoryDescription.Text;
                string categoryDescriptionEng = txtFullCategoryDescription.Text.Trim();
                int sortdisplay = Convert.ToInt32(txtSortDisplay.Text.Trim());
                int parent = Convert.ToInt32(ddlParent.SelectedValue);
                int content_type = Convert.ToInt32(ddlContentType.SelectedValue);
                filename = base.Upload("productscategories", filename, fupIcon, divMessage, false);
                string result = BO_portal_categories.Update(Id,
                        parent, content_type,
                        string.Empty, string.Empty, categoryName,
                        categoryNameEng, filename, string.Empty, string.Empty, categoryDescription, categoryDescriptionEng, cboMenu.Checked, 0, sortdisplay,
                        Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]),
                        ObjDT.Rows[0]["CREATED_BY"].ToString(),
                        DateTime.Now, Administrator.USER_NAME, 1);
                if (result.Equals("0"))
                    divMessage.InnerHtml = MessageBox.Error("Thông Báo", "Chưa thực hiện được, vui lòng trở lại sau !");
                else
                {
                    divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thực hiện thành công !");
                    Response.Redirect("default.aspx?control=Categories&category_id=" + Category_ID.ToString() + "&keyword=" + Keyword);
                }
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi: <br />" + ex.Message); }
    }
}
